/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.meego.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author TueHM
 */
public class Validations {

    private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    private static final String USERNAME_PATTERN = "^[a-zA-Z0-9_-]{4,32}$";
    private static final String PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z_-])(?=.*[A-Z])(?=.*[/,:<>!~@#$%^&\\*()+=?()\"|!\\[]).{6,})";
    private static final String URL_PATTERN = "^(https?://)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\?=.-]*)*\\/?$";
    private static final String VIETNAM_PHONE_PATTERN = "^(0|\\+84)([1-9]{1})([0-9]{8,9})$";
    private static Pattern emailPattern;
    private static Pattern usernamePattern;
    private static Pattern passwordPattern;
    private static Pattern urlPattern;
    private static Pattern phonePattern;

    static {
        emailPattern = Pattern.compile(EMAIL_PATTERN);
        usernamePattern = Pattern.compile(USERNAME_PATTERN);
        passwordPattern = Pattern.compile(PASSWORD_PATTERN);
        urlPattern = Pattern.compile(URL_PATTERN);
        phonePattern = Pattern.compile(VIETNAM_PHONE_PATTERN);
    }

    public static boolean isValidEmail(final String email) {
        Matcher matcher = emailPattern.matcher(email);
        return matcher.matches();
    }

    public static boolean isValidUsername(final String username) {
        Matcher matcher = usernamePattern.matcher(username);
        return matcher.matches();
    }

    public static boolean isStrongPassword(final String password) {
        Matcher matcher = passwordPattern.matcher(password);
        return matcher.matches();
    }

    public static boolean isValidUrl(final String uri) {
        Matcher matcher = urlPattern.matcher(uri);
        return matcher.matches();
    }

    public static boolean isValidPhone(final String phone) {
        Matcher matcher = phonePattern.matcher(phone);
        return matcher.matches();
    }
        
    public static boolean isValidDate(String date){
        try {
            DateUtils dateUtils = new DateUtils(date);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    
    
    public static void main(String[] args) {
//        try {
//            DateUtils a = new DateUtils("04/02/1983");
//            System.out.println(a.getDate());
//            System.out.println(new Date(a.getTime()));
//        } catch (ParseException ex) {
//            Logger.getLogger(Validations.class.getName()).log(Level.SEVERE, null, ex);
//        }
        Pattern aa = Pattern.compile("web-management.(.*).html");
        Matcher matcher = aa.matcher("web-management.fdafdsaf.html");
//        System.out.println(matcher.groupCount());
        System.out.println(matcher.matches());
    }
}
